package behavior.observer.self;

import lombok.Data;

import java.util.LinkedList;
import java.util.List;

@Data
public class ChiBiSubject implements WarfareSubject {

    private List<WarfareObserver> observers = new LinkedList<>();

    public ChiBiSubject(String name, Integer solidersNum) {
        this.name = name;
        this.solidersNum = solidersNum;
    }

    private String name;

    private Integer solidersNum;


    @Override
    public void add() {
        solidersNum++;
        notifyObservers("增加士兵：士兵数：" + solidersNum);
    }

    @Override
    public void remove() {
        solidersNum--;
        notifyObservers("减少士兵：士兵数：" + solidersNum);
    }

    @Override
    public Integer getSoldierCount() {
        return solidersNum;
    }

    @Override
    public void addObserver(WarfareObserver observer) {
        observers.add(observer);
    }

    @Override
    public void deleteObserver(WarfareObserver observer) {
        observers.remove(observer);
    }

    @Override
    public void deleteObservers() {
        observers.clear();
    }

    @Override
    public void notifyObservers(Object arg) {
        for (WarfareObserver observer : observers) {
            System.out.println("通知观察者：" + observer.getName() + "，更新状态：" + arg);
            observer.update(arg);
        }

    }
}
